package com.ywu.boot.interceptor.impl;

import com.ywu.boot.context.YwuContext;
import com.ywu.boot.context.YwuContextHolder;
import com.ywu.boot.context.YwuContextRequest;
import com.ywu.boot.interceptor.ContextBuilderInterface;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @ClassName RequestInfoContextBuilder
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/3/17 17:33
 * @Version 1.0
 **/
@Component
@Order(1)
public class RequestInfoContextBuilder implements ContextBuilderInterface {
    @Override
    public void buildContext(HttpServletRequest request, HttpServletResponse response) {
        StringBuffer requestURL = request.getRequestURL();
        String method = request.getMethod();
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        YwuContextRequest ywuContextRequest = currentContext.getYwuContextRequest();
        if (Objects.isNull(ywuContextRequest)) {
            ywuContextRequest = new YwuContextRequest();
        }
        ywuContextRequest.setMethod(method).setRequestUrl(requestURL.toString());
        currentContext.setYwuContextRequest(ywuContextRequest);
    }

    @Override
    public String getBuildHeaderKey() {
        return null;
    }
}
